package org.example.controller;


import jakarta.annotation.Resource;
import org.example.pojo.Voice;
import org.example.service.TextToSpeechService;
import org.example.service.VoiceService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * 文本转语音控制器
 *
 * @author rjs
 */
@RestController
@RequestMapping("/text-to-speech")
public class TextToSpeechController {

    /**
     * 文本转语音服务对象
     */
    @Resource
    private TextToSpeechService ttsService;

    /**
     * 语音文件服务对象
     */
    @Resource
    private VoiceService voiceService;

    /**
     * 转换文本为语音并保存
     *
     * @param text 要转换的文本
     * @return 操作结果
     * @throws IOException 输入输出异常
     */
    @PostMapping
    public ResponseEntity<String> convertAndSave(@RequestBody String text) throws IOException {

        // 调用文本转语音服务转换文本
        byte[] audio = ttsService.convert(text);

        // 封装语音数据并保存
        Voice voice = new Voice();
        voice.setVoice(audio);
        voiceService.save(voice);

        // 返回成功响应
        return new ResponseEntity<>("音频成功保存", HttpStatus.OK);
    }

}


